package com.likai.blog.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.likai.blog.pojo.Blog;
import com.likai.blog.service.IBlogService;
import com.likai.blog.utils.AnswerJson;
import com.likai.blog.utils.PageList;
import com.likai.blog.utils.StringUtil;

@Controller
@RequestMapping(value="/admin/blog")
public class BlogAdminController {

	@Autowired
	@Qualifier("blogService")
	private IBlogService blogService ;
	
	@ResponseBody
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ResponseEntity<AnswerJson> update(
			Blog po
			) {
		AnswerJson json = new AnswerJson() ;
		int result = blogService.updateInfo(po) ;
		if(result != 1) {
			json.setData("添加失败");
			return new ResponseEntity<AnswerJson>(json,HttpStatus.CONFLICT) ;
		}
		json.setData("添加成功");
		return new ResponseEntity<AnswerJson>(json,HttpStatus.OK) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ResponseEntity<PageList> blogList(
			Integer page,
			Integer rows,
			String title
			) {
		Blog po = new Blog() ;
		if(StringUtil.isNotEmpty(title)) {
			po.setTitle(title);
		}
		PageHelper.startPage(page, rows) ;
		List<Blog> blogList = blogService.findList(po) ;
		
		PageInfo pageInfo = new PageInfo(blogList,5) ;
		
		PageList pageList = new PageList(pageInfo.getTotal(),pageInfo.getList()) ; 
		
		return new ResponseEntity<PageList>(pageList,HttpStatus.OK) ;
		
	}
	
	/**
	 * 批量删除博客信息
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/remove/{ids}",method=RequestMethod.DELETE)
	public ResponseEntity<AnswerJson> remove(
			@PathVariable(value="ids") String [] ids
			) {
		AnswerJson json = new AnswerJson() ;
		int result = blogService.deleteByKey(ids) ;
		if(result == 1) {
			json.setData("删除成功");
			return new ResponseEntity<AnswerJson>(json,HttpStatus.OK) ;
		} else {
			json.setData("删除失败");
			return new ResponseEntity<AnswerJson>(json,HttpStatus.OK) ;
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/minute",method=RequestMethod.POST)
	public ResponseEntity<AnswerJson> minute(
			String blogId
			) {
		AnswerJson json = new AnswerJson() ;
		
		Blog po = blogService.findByBlogId(blogId) ;
		
		json.setData(po);
		
		return new ResponseEntity<AnswerJson>(json,HttpStatus.OK) ;
	}
	
	
}
